package com.share.finance.risk.rules.web;

import com.share.common.common.Page;
import com.share.common.common.Result;
import com.share.finance.risk.rules.model.db.ProjectHist;
import com.share.finance.risk.rules.model.vo.ProjectHistAddOrUpdateReq;
import com.share.finance.risk.rules.model.vo.ProjectHistPageReq;
import com.share.finance.risk.rules.service.IProjectHistService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 工程表  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-11-24
 */
@RequestMapping(value = "/projecthist")
@RestController
public class ProjectHistController {

    @Autowired
    private IProjectHistService projectHistService;

    @ApiOperation(value = "保存流程")
    @RequestMapping(value = "/saveFlow")
    public Result saveFlow(String strategyIds, String version, Long projectId, Boolean isAdd) {
        if (StringUtils.isEmpty(strategyIds) || projectId == null) {
            return Result.failure("strategyIds或者projectId不能为空");
        }
        List<Long> list = Arrays.stream(strategyIds.split(",")).filter(e -> StringUtils.isNotBlank(e))
                .map(e -> Long.parseLong(e)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(list)) {
            return Result.failure("strategyIds填写异常");
        }
        Integer status = projectHistService.saveFlow(projectId, version, list,isAdd);
        return Result.insertOrDeleteOrUpdate(status);
    }

    @ApiOperation(value = "获取所有工程版本")
    @RequestMapping(value = "/pageList")
    public Result<Page<ProjectHist>> pageList(ProjectHistPageReq req) {
        Page<ProjectHist> page = projectHistService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("新增")
    @RequestMapping("/add")
    public Result add(ProjectHistAddOrUpdateReq req) {
        ProjectHist record = new ProjectHist();
        BeanUtils.copyProperties(req, record);
        return Result.bool(projectHistService.save(record));
    }

    @ApiOperation("修改")
    @RequestMapping("/update")
    public Result update(ProjectHistAddOrUpdateReq req) {
        if (req.getId() == null) {
            return Result.failure("参数异常!");
        }
        ProjectHist record = new ProjectHist();
        BeanUtils.copyProperties(req, record);
        return Result.bool(projectHistService.updateById(record));
    }

    @ApiOperation("删除")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        return Result.bool(projectHistService.removeById(id));
    }
}
